<?php

namespace app\admin\controller\dts\material\used;

use app\common\controller\Backend;
use think\Db;

/**
 * 食材物资领用记录
 * @icon fa fa-circle-o
 */
class Food extends Backend
{

    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $defaultJoin = [
                ['dts_approve_food_let let', 'let.id = log.food_let_id and let.status = 5'],
                ['dts_material_food food', 'food.id = log.material_food_id', 'left'],
                ['admin create', 'create.id = let.admin_id', 'left'],
            ];
            
            $list = Db::name('dts_approve_food_log')->alias('log')
            ->field('log.*,let.createtime as let__createtime,food.name as food__name,food.unity as food__unity,food.specs as food__specs,create.nickname as create__nickname')
            ->join($defaultJoin)
            ->where($where)
            ->order('let.createtime', 'desc')
            ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => paginateList($list));

            return json($result);
        }

        return $this->view->fetch();
    }
}